package com.tjnmt.togo.activities;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tjnmt.togo.R;
import com.tjnmt.togo.adapter.MainFragmentPageAdapter;
import com.tjnmt.togo.fragments.GrabFragment;
import com.tjnmt.togo.fragments.MineFragment;
import com.tjnmt.togo.fragments.TaskFragment;
import com.tjnmt.togo.utils.ResourceManagerUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动主页面。
 */
@ContentView(value = R.layout.activity_main)
public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    @ViewInject(value = R.id.ll_grab)
    private LinearLayout ll_grab;
    @ViewInject(value = R.id.ll_task)
    private LinearLayout ll_task;
    @ViewInject(value = R.id.ll_mine)
    private LinearLayout ll_mine;

    @ViewInject(value = R.id.tv_grab)
    private TextView tv_grab;
    @ViewInject(value = R.id.tv_task)
    private TextView tv_task;
    @ViewInject(value = R.id.tv_mine)
    private TextView tv_mine;

    @ViewInject(value = R.id.iv_grab)
    private ImageView iv_grab;
    @ViewInject(value = R.id.iv_task)
    private ImageView iv_task;
    @ViewInject(value = R.id.iv_mine)
    private ImageView iv_mine;

    @ViewInject(value = R.id.pager_main)
    private ViewPager viewPager;

    private List<Fragment> fragments;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);

        initView();
    }

    private void initView(){

        fragments = new ArrayList<Fragment>();

        fragments.add(GrabFragment.getInstance());
        fragments.add(TaskFragment.getInstance());
        fragments.add(MineFragment.getInstance());

        viewPager.setAdapter(new MainFragmentPageAdapter(super.getSupportFragmentManager(),fragments,null));
        viewPager.addOnPageChangeListener(this);
    }

    @Event(value = {R.id.ll_grab,R.id.ll_mine,R.id.ll_task})
    private void getEvent(View v){

        restartOption();

        switch (v.getId()){
            case R.id.ll_grab:
                tv_grab.setTextColor(ResourceManagerUtil.getColor(R.color.green));
                iv_grab.setImageDrawable(ResourceManagerUtil.getDrawable(R.drawable.grab_1));
                viewPager.setCurrentItem(0);
                break;
            case R.id.ll_task:
                tv_task.setTextColor(ResourceManagerUtil.getColor(R.color.green));
                iv_task.setImageDrawable(ResourceManagerUtil.getDrawable(R.drawable.task_1));
                viewPager.setCurrentItem(1);
                break;
            case R.id.ll_mine:
                tv_mine.setTextColor(ResourceManagerUtil.getColor(R.color.green));
                iv_mine.setImageDrawable(ResourceManagerUtil.getDrawable(R.drawable.mine_1));
                viewPager.setCurrentItem(2);
                break;
            default:
                break;
        }
    }

    private void restartOption(){
        tv_grab.setTextColor(ResourceManagerUtil.getColor(R.color.hint));
        tv_task.setTextColor(ResourceManagerUtil.getColor(R.color.hint));
        tv_mine.setTextColor(ResourceManagerUtil.getColor(R.color.hint));

        iv_grab.setImageDrawable(ResourceManagerUtil.getDrawable(R.drawable.grab_0));
        iv_task.setImageDrawable(ResourceManagerUtil.getDrawable(R.drawable.task_0));
        iv_mine.setImageDrawable(ResourceManagerUtil.getDrawable(R.drawable.mine_0));
    }

    //#############################################
    //##            对外暴露的方法               ##
    //#############################################

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

        restartOption();

        switch (viewPager.getCurrentItem()){
            case 0:
                tv_grab.setTextColor(ResourceManagerUtil.getColor(R.color.green));
                iv_grab.setImageDrawable(ResourceManagerUtil.getDrawable(R.drawable.grab_1));
                break;
            case 1:
                tv_task.setTextColor(ResourceManagerUtil.getColor(R.color.green));
                iv_task.setImageDrawable(ResourceManagerUtil.getDrawable(R.drawable.task_1));
                break;
            case 2:
                tv_mine.setTextColor(ResourceManagerUtil.getColor(R.color.green));
                iv_mine.setImageDrawable(ResourceManagerUtil.getDrawable(R.drawable.mine_1));
                break;
            default:
                break;
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    public static void actionStart(Context context){

        Intent intent = new Intent(context,MainActivity.class);
        context.startActivity(intent);

    }

}
